package com.njxz.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.njxz.web.pojo.Page3;
import com.njxz.web.pojo.Users;
import com.njxz.web.service.UserService;
import com.njxz.web.service.Impl.UserServiceImpl;

/**
 * Servlet implementation class GetUsers
 */
@WebServlet("/GetUsers")
public class GetUsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetUsersServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		Integer page=Integer.valueOf(request.getParameter("page")); 
		Integer rows=Integer.valueOf(request.getParameter("rows")); 
//		System.out.println(page + "--" + rows);
		PrintWriter out = response.getWriter();
		UserService serviceImpl;
		try {
			serviceImpl = new  UserServiceImpl();
			//String msg = serviceImpl.selectAllUser(page, rows);
			List<Users> list = serviceImpl.select((page-1)*rows+1, rows*page);
			if(null!=list)
			{
				int total=serviceImpl.queryTotal();
				System.out.println(total);
				Page3 p = new Page3();
				p.setRows(list);;
				p.setTotal(total);
				String msg=JSON.toJSONString(p);
				out.print(msg); 
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
